using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAOLayer
{
    public class LoaiPhongDao
    {
        public static DataTable LayDanhSachLoaiPhong()
        {
            DataTable dt = new DataTable();
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Select * From LOAIPHONG";
            //'B4: Thuc thi chuoi strSQL
            OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            // 'B5: Dong ket noi CSDL
            cn.Close();
            return dt;

        }
        public static LoaiPhongDto LayMotLoaiPhong(int ma)
        {
            LoaiPhongDto lphong = new LoaiPhongDto();
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Select * From LOAIPHONG Where MaLoaiPhong = ? ";
            //'B4: Thuc thi chuoi strSQL
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaLoaiPhong", OleDbType.Integer);
            cmd.Parameters[("@MaLoaiPhong")].Value = ma;
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                lphong.MaLoaiPhong = (int)dr["MaLoaiPhong"];
                lphong.TenLoaiPhong = (string)dr["TenLoaiPhong"];
                lphong.DonGia = (int)dr["DonGiaPhong"];

            }
            cn.Close();
            return lphong;

        }
        public static void Them(LoaiPhongDto lp)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Insert into LOAIPHONG ( TenLoaiPhong , DonGiaPhong ) values(?,?)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenLoaiPhong", OleDbType.WChar);
            cmd.Parameters["@TenLoaiPhong"].Value = lp.TenLoaiPhong;
            cmd.Parameters.Add("@DonGiaPhong", OleDbType.Integer);
            cmd.Parameters["@DonGiaPhong"].Value = lp.DonGia;
            cmd.ExecuteNonQuery();

            strSQL = "Select @@IDENTITY";
            cmd = new OleDbCommand(strSQL, cn);
            lp.MaLoaiPhong = Convert.ToInt32(cmd.ExecuteScalar());
            // B5: Dong ket noi CSDL
            cn.Close();
        }
        public static void CapNhatXuongCSDL(LoaiPhongDto p)
        {
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Update LOAIPHONG Set TenLoaiPhong = ? ,DonGiaPhong = ?  Where MaLoaiPhong = ?  ";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenLoaiPhong", OleDbType.WChar);
            cmd.Parameters["@TenLoaiPhong"].Value = p.TenLoaiPhong;
            cmd.Parameters.Add("@DonGiaPhong", OleDbType.Integer);
            cmd.Parameters["@DonGiaPhong"].Value = p.DonGia;
            cmd.Parameters.Add("@MaLoaiPhong", OleDbType.Integer);
            cmd.Parameters[("@MaLoaiPhong")].Value = p.MaLoaiPhong;
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public static void Xoa(int MaLPhong)
        {
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            // B3: Tao chuoi strSQL thao tac CSDL
            string strSQL = "Delete From LOAIPHONG Where MaLoaiPhong = ?";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaLoaiPhong", OleDbType.Integer);
            cmd.Parameters["@MaLoaiPhong"].Value = MaLPhong;

            cmd.ExecuteNonQuery();
            // B5: Dong ket noi CSDL
            cn.Close();
        }
        
       
    }
}
